package com.ccy.legendary.Operation;

import com.ccy.legendary.Operation.pojo.ActRemark;
import com.ccy.legendary.base.BaseController;
import com.ccy.legendary.base.pojo.ResultBean;
import com.ccy.legendary.base.utils.CollectionUtils;
import com.ccy.legendary.socker.WSMessageService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.List;

/**
 * 操作请求的controller
 */
@Controller
@RequestMapping("/operation")
public class OperationController extends BaseController {

    private final JudgeService judgeService;
    private final WSMessageService wsMessageService;


    public OperationController(@Qualifier("judgeServiceTest") JudgeService judgeService,WSMessageService wsMessageService) {
        this.judgeService = judgeService;
        this.wsMessageService = wsMessageService;
    }

    @RequestMapping("/prepare")
    public ResultBean prepare(@RequestParam("userCode")String userCode) throws IOException {
        ResultBean result = new ResultBean();
        result.addMessage(ActRemark.preRemark(userCode));
        result.addResultBean(judgeService.startJudge());
        wsMessageService.sendAll(result.getMessages());
        return result;
    }

    /**
     * 回合开始阶段
     * @param userCode 当前玩家
     * @return ResultBean
     */
    @RequestMapping("/start")
    public ResultBean start(@RequestParam("userCode")String userCode) {
        ResultBean result = new ResultBean();
        result.addMessage(ActRemark.startRemark(userCode));
        List<String> messages = judgeService.ackJudge(userCode);
        if(CollectionUtils.isNotBlank(messages))
            result.addMessages(messages);
        return result;
    }

    /**
     * 掷骰子阶段
     * @param userCode 用户code
     * @param point 点数
     * @return ResultBean
     */
    @RequestMapping("/run")
    public ResultBean run(@RequestParam("userCode")String userCode,@RequestParam("point")Integer point) {
        ResultBean result = new ResultBean();
        List<String> messages = judgeService.runJudge(userCode,point);
        if(CollectionUtils.isNotBlank(messages))
            result.addMessages(messages);
        return result;
    }
}
